package com.magic.zhixin.service;

import com.magic.zhixin.entity.*;
import com.magic.zhixin.model.QuestionStatus;
import com.magic.zhixin.repository.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class RedisService {
    @Autowired
    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    private QuestionBankRepository questionBankRepository;
    @Autowired
    private QuestionRepository questionRepository;
    @Autowired
    private AnswerRepository answerRepository;
    @Autowired
    private UserQuestionRepository userQuestionRepository;
    @Autowired
    private UserQuestionDetailRepository userQuestionDetailRepository;
    @Autowired
    private CopywritingRepository copywritingRepository;

    /**
     * 根据类型获取题库 1：我的 2：系统
     * @param type
     * @return
     */
    public List<QuestionBank> getQuestionBank(Integer type,Long userId){
        ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
        List<QuestionBank> questionBanks = null;
        Object obj = null;
        if(type == 1){
            //用户自定义
           obj = opsForValue.get("CustomQuestionBank");
           if(obj != null){
                questionBanks = (List<QuestionBank>) obj;
            } else {
                questionBanks = questionBankRepository.findByCreateUserId(userId);
                opsForValue.set("CustomQuestionBank",questionBanks);
            }
        } else {
            obj =  opsForValue.get("SystemQuestionBank");
            if(obj != null){
                questionBanks = (List<QuestionBank>) obj;
            } else {
                questionBanks = questionBankRepository.findByCreateUserIdIsNull();
                opsForValue.set("SystemQuestionBank",questionBanks);
            }
        }
        return questionBanks;
    }

    /**
     * 根据题库获取所有问题
     * @param questionBankId
     * @return
     */
    public List<Question> getQuestionByQuestionBankId(Long questionBankId){
        String key = "Question-" + questionBankId;
        ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
        List<Question> questions= null;
        Object obj = null;
        //用户自定义
        obj = opsForValue.get(key);
        if(obj != null){
            questions = (List<Question>) obj;
        } else {
            questions = questionRepository.findByQuestionBankId(questionBankId);
            opsForValue.set(key,questions);
        }

        questions.forEach( question -> {
            List<Answer> answers = answerRepository.findByQuestionId(question.getId());
            question.setAnswers(answers);
        });

        return questions;
    }

    /**
     * 获取用户所有的提问
     * @param userId
     * @return
     */
    public List<UserQuestion> getUserQuestion(Long userId){
        String key = "UserQuestion-" + userId;
        ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
        List<UserQuestion> userQuestions= null;
        Object obj = null;
        obj = opsForValue.get(key);
        if(obj != null){
            userQuestions = (List<UserQuestion>) obj;
        } else {
            userQuestions = userQuestionRepository.findByUserIdAndQuestionStatus(userId, QuestionStatus.SUCCESS);
            opsForValue.set(key,userQuestions);
        }
        return userQuestions;
    }

    /**
     * 获取用户问题答案
     * @param userQuestionId
     * @return
     */
    public List<UserQuestionDetail> getUserQuestionDetail(Long userQuestionId){
        String key = "UserQuestionDetai-" + userQuestionId;
        ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
        List<UserQuestionDetail> userQuestionDetailList= null;
        Object obj = null;
        obj = opsForValue.get(key);
        if(obj != null){
            userQuestionDetailList = (List<UserQuestionDetail>) obj;
        } else {
            userQuestionDetailList = userQuestionDetailRepository.findByUserQuestionId(userQuestionId);
            opsForValue.set(key,userQuestionDetailList);
        }
        return userQuestionDetailList;
    }

    /**
     * 删除redis缓存
     * @param key
     */
    public void delete(String key){
        redisTemplate.delete(key);
    }

    public List<Copywriting> getCopywriting(Integer type) {
        String key = "Copywriting";
        ValueOperations<String, Object> opsForValue = redisTemplate.opsForValue();
        List<Copywriting> copywritings= null;
        Object obj = null;
        obj = opsForValue.get(key);
        if(obj != null){
            copywritings = (List<Copywriting>) obj;
        } else {
            copywritings = copywritingRepository.findByTypeAndStatus(type,0);
            opsForValue.set(key,copywritings);
        }
        return copywritings;
    }
}
